// 职责：负责执行各种各样的sql语句，不负责执行逻辑
// const mysql = require('mysql');
// function execSql(sql, callbackFn) {
//     let conn = mysql.createConnection({
//         host: '127.0.0.1',
//         port: 3306,
//         user: 'root',
//         password: 'root',
//         database: 'hm_project1'
//     });

//     conn.connect();

//     conn.query(sql, callbackFn);

//     conn.end();
// };
const mysql = require('mysql2');
async function execSql(sql) {
    // const mysql = require('mysql2/promise');
    // 创建连接
    // const conn = await mysql.createConnection({host:'127.0.0.1',port: 3306, user: 'root', password:'root', database: 'hm_project1'});
  
    // 数据库操作
    // const [rows, fields] = await conn.execute(sql);

    // create the pool
      const pool = mysql.createPool({host:'127.0.0.1',port: 3306, user: 'root', password:'root', database: 'hm_project1'});
      // now get a Promise wrapped instance of that pool
      const promisePool = pool.promise();
      // query database using promises
      const [rows,fields] = await promisePool.query(sql);
      promisePool.end();
      return rows;
  }

// 导出的本质上是一个：execSql(sql, callbackFn)
module.exports = execSql;